/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao.benh;

import java.util.List;
import model.dao.AbstractQuery;
import model.dao.ProjectQuery;
import model.entity.Benh;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;


/**
 *
 * @author Admin
 */
public class BenhDAO {
    private static final String LOAD_1_BENH = "FROM Benh benh WHERE benh.benhMa =";
//    public static final void insertBenh(Benh benh){
//        AbstractQuery.insertObject(benh);
//    }
    public static final int insertBenh(Benh benh){
        Session session = HibernateUtil.getSessionFactory().openSession();
        int id;
        try {
            session.beginTransaction();
            session.save(benh);
            session.getTransaction().commit();
            id = benh.getBenhMa();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw  new HibernateException(e);
        }finally{
            session.close();
        }
        return  id;
    }
    
     public static  void deleteBenh(int benhMa){
        AbstractQuery.modifyObject(3, AbstractQuery.loadObject(LOAD_1_BENH + benhMa).get(0));
    }
     /**
      * D8ể load xuống csdl
      * @param benhMa
      * @return 
      */
     public static final List<Benh> timKiemTheoMaBenh(int benhMa){
        List list = AbstractQuery.loadObject(ProjectQuery.LOAD_1_BENH + benhMa);
        return list;
        
}
     /**
      * Update Bệnh
      */
     public static final void update(Benh benh){
         Session session = HibernateUtil.getSessionFactory().openSession();
            try {
                session.beginTransaction();
                session.update(benh);
                session.getTransaction().commit();
            } catch (HibernateException e) {
                session.getTransaction().rollback();
                throw new HibernateException(e);
            }finally{
                session.close();
            }
     }
}